package com.fanfan.SmartPensionSystembackend.controller;

import com.fanfan.SmartPensionSystembackend.entity.Bed;
import com.fanfan.SmartPensionSystembackend.service.BedService;
import com.fanfan.SmartPensionSystembackend.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description BedController
 * @Author Guangdi Chen
 * @Date 2025-03-11
 */
@RestController
@RequestMapping("/stay")
@CrossOrigin(origins = "*", maxAge = 3600)
public class BedController {
    @Autowired
    private BedService bedService;

    // 添加床位
    @PostMapping("/addBed")
    public Map<String, Object> addBed(@RequestBody List<Bed> beds) {
        try {
            boolean result = bedService.addBeds(beds);
            if (result) {
                return ResponseUtil.success("添加成功", null);
            } else {
                return ResponseUtil.error("添加失败：数据保存异常");
            }
        } catch (Exception e) {
            return ResponseUtil.error("添加失败：" + e.getMessage());
        }
    }
    // 获取所有床位信息
    @GetMapping("/getAllBeds")
    @CrossOrigin
    public Map<String, Object> getAllBeds() {
        try {
            List<Bed> beds = bedService.getAllBedList();
            return ResponseUtil.success("查询成功", beds);
        } catch (Exception e) {
            return ResponseUtil.error("查询失败：" + e.getMessage());
        }
    }
    //删除床位
    @DeleteMapping("/deleteBed")
    @CrossOrigin
    public Map<String, Object> deleteBed(@RequestParam String bedId) {
        try {
            System.out.println("删除bedId = " + bedId);
            boolean result = bedService.deleteBed(bedId);
            if (result) {
                return ResponseUtil.success("删除成功", null);
            } else {
                return ResponseUtil.error("删除失败：数据删除异常");
            }
        } catch (Exception e) {
            return ResponseUtil.error("删除失败：" + e.getMessage());
        }
    }
    //获取筛选床位
    @GetMapping("/screenBeds")
    @CrossOrigin
    public Map<String, Object> screenBeds(@RequestParam String buildingNo, @RequestParam String floorNo, @RequestParam String roomNo) {
        try {
            List<Bed> beds = bedService.screenBeds(buildingNo, floorNo, roomNo);
            return ResponseUtil.success("筛选成功", beds);
        } catch (Exception e) {
            return ResponseUtil.error("筛选失败：" + e.getMessage());
        }
    }

}